package com.liwei.vip.mall.goods.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liwei.vip.mall.goods.mapper.BrandMapper;
import com.liwei.vip.mall.goods.model.Brand;
import com.liwei.vip.mall.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /***
     * 条件查询
     * @param brand
     */
    @Override
    public List<Brand> queryList(Brand brand) {
//        条件包装对象
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
//        根据name查询品牌
        queryWrapper.like("name", brand.getName());
//        根据initial查询
        queryWrapper.eq("initial", brand.getInitial());

        return brandMapper.selectList(queryWrapper);
    }

    /**
     * 条件分页查询
     * return Page<Brand>
     *
     * @param brand
     * @param currentPage
     * @param pageSize
     */
    @Override
    public Page<Brand> queryPageList(Brand brand, Long currentPage, Long pageSize) {
        //条件包装对象
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        //根据name查询品牌
        queryWrapper.like("name", brand.getName());
        return brandMapper.selectPage(new Page<Brand>(currentPage, pageSize), queryWrapper);
    }

    /**
     * 根据分类ID集合查询每个品牌信息
     *
     * @param id：分类ID
     */
    @Override
    public List<Brand> queryByCategoryId(Integer id) {
        //根据分类父ID查询品牌ID集合
        List<Integer> brandIds = brandMapper.queryBrandIds(id);
        //根据品牌ID集合查询品牌集合
        if(brandIds!=null && brandIds.size()>0){
            return brandMapper.selectList(new QueryWrapper<Brand>().in("id", brandIds));
        }
        return null;
    }
}
